package com.study.exam.service.impl;

import com.study.exam.pojo.Department;
import com.study.exam.mapper.DepartmentMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * 部门表(Department)表服务实现类
 *
 * @author thinknovo
 * @since 2025-01-18 10:57:01
 */
@Service("departmentService")
public class DepartmentServiceImpl {
    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", this.departmentMapper.selectById(id));
        map.put("msg", "查询单条记录成功");
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "全部查询成功");
        map.put("data", this.departmentMapper.selectAll());
        return map;
    }

    /**
     * 分页查询
     *
     * @param department 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(Department department, int page, int limit) {
        // page 页码   (page-1)*limit=起始下标index
        int startIndex = (page - 1) * limit;   // 计算出起始下标
        List<Department> list = this.departmentMapper.selectByLimit(department, startIndex, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        
        // 获取满足条件的总条数
        long count = this.departmentMapper.selectCount(department);
        // 计算总页数
        long maxPage = (count - 1) / limit + 1;
        
        map.put("count", count);
        map.put("maxPage", maxPage);
        map.put("nowPage", page);
        map.put("data", list);
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(Department department) {
        this.departmentMapper.insert(department);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", department);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 修改数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(Department department) {
        this.departmentMapper.updateById(department);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", department);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("id", id);
        map.put("msg", this.departmentMapper.deleteById(id) > 0 ? "删除成功" : "删除失败");
        return map;
    }
}
